package com.hyst.web;

import java.util.List;
import java.util.Map;

import javax.naming.ldap.PagedResultsControl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hyst.common.ResultBean;
import com.hyst.common.ResultPage;
import com.hyst.domain.Teacher;
import com.hyst.domain.TeacherStudent;
import com.hyst.service.TeacherService;

/**
 * @Description:外教表controller
 * @author：dy 
 *@Date：2018年08月21日
 */
@RestController
@RequestMapping("teachers")
@CrossOrigin(maxAge = 3600, origins = "*")
public class TeacherController {
	@Autowired
	private TeacherService teacherService;
	
	@GetMapping
	public ResultPage<Teacher> list(Teacher teacher) {
		return ResultPage.success(teacherService.list(teacher));
	}

	@PostMapping
	public ResultBean<Teacher> creat(final Teacher teacher) {
		return ResultBean.success(teacherService.insert(teacher));
	}
	
	@PostMapping("state/{uniqueid}")
	public ResultBean<Teacher> state(final Teacher teacher) {
		return ResultBean.success(teacherService.state(teacher));
	}

	/**
	 * @Description: 根据主键查询
	 * @param uniqueid
	 * @return ResultBean<Teacher>
	 */
	@GetMapping("{uniqueid}")
	public ResultBean<Teacher> getByPriKey(@PathVariable("uniqueid") final String uniqueid) {
		return ResultBean.success(teacherService.getByPriKey(uniqueid));
	}

	/**
	 * @Description: 根据主键更新全字段
	 * @param teacher
	 * @return ResultBean<Teacher>
	 */
	@PatchMapping("{uniqueid}")
	public ResultBean<Teacher> update(final Teacher teacher) {
		return ResultBean.success(teacherService.update(teacher));
	}

	/**
	 * @Description: 根据主键更新不为默认值的字段
	 * @param teacher
	 * @return ResultBean<Teacher>
	 */
	//@PutMapping("{uniqueid}")
	@PostMapping("{uniqueid}")
	public ResultBean<Teacher> updatePatch(@PathVariable("uniqueid") final String uniqueid,
			final Teacher teacher) {
		return ResultBean.success(teacherService.updateSelective(teacher));
	}

	/**
	 * @Description: 根据主键删除
	 * @param uniqueid
	 * @return ResultBean<Teacher>
	 */
	@DeleteMapping("{uniqueid}")
	public ResultBean<Teacher> delete(@PathVariable("uniqueid") final String uniqueid) {
		return ResultBean.success(teacherService.delete(uniqueid));
	}

	//绑定下拉用
	@GetMapping("select2")
	@ResponseBody
	public List<Map> getSelect2Data() {
		return teacherService.getSelect2Data();
	}

	//绑定下拉用
	@GetMapping("select3")
	@ResponseBody
	public List<Map> getSelect3Data(@RequestParam("key") String key) {
		List<Map> select3Data = teacherService.getSelect3Data(key);
		if(!select3Data.isEmpty()){
			return select3Data;
		}
		return teacherService.getSelect2Data();
	}


	@GetMapping("students")
	public ResultPage<TeacherStudent> getMyStudents(String teacherId){
		return ResultPage.success(teacherService.getMyStudents( teacherId));
	}
}
